package zhao.tx.eecs.zhao;

import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.andware.volley.VolleyError;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import zhao.tx.eecs.JsonModel.MessageModel;
import zhao.tx.eecs.R;
import zhao.tx.eecs.Service.LocationService;
import zhao.tx.eecs.Service.LocationService.gpsInterface;
import zhao.tx.eecs.Service.WatchdogService;
import zhao.tx.eecs.Tools.VolleyBase;
import zhao.tx.eecs.common.CommonActivity;
import zhao.tx.eecs.dialog.MoreDialog;

public class MainFrm extends CommonActivity implements gpsInterface {
    private ListAdapter adapter = null;
    private List< Map< String, Object > > list = null;//数据

    private ImageView btntool;

    private MoreDialog moreDialog;

    private List<String> dialogTitleList = new ArrayList<> (  );

    private Handler handler = new Handler () {//http返回的处理
        @Override
        public void handleMessage ( Message msg ) {

        }
    };

    @Override
    public void onCreate ( Bundle savedInstanceState ) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.mainfrm );


        super.listViewAll = ( ListView ) findViewById ( android.R.id.list );

        btntool = ( ImageView ) findViewById ( R.id.btntool );
        menu1 = ( LinearLayout ) findViewById ( R.id.menu1 );
        menu1.setOnTouchListener ( view1 );
        menu2 = ( LinearLayout ) findViewById ( R.id.menu2 );
        menu2.setOnTouchListener ( view2 );
        menu3 = ( LinearLayout ) findViewById ( R.id.menu3 );
        menu3.setOnTouchListener ( view3 );
        if ( !LocationService.isWorked ( this ) ) {
            Intent intent = new Intent ( this , LocationService.class );
            startService ( intent );
        }
        View xhx1 = ( View ) findViewById ( R.id.xhx1 );
        View xhx2 = ( View ) findViewById ( R.id.xhx2 );
        View xhx3 = ( View ) findViewById ( R.id.xhx3 );
        xhx1.setVisibility ( View.VISIBLE );
        xhx2.setVisibility ( View.GONE );
        xhx3.setVisibility ( View.GONE );

        dialogTitleList.add ( "退出程序" );

        moreDialog = new MoreDialog ( this,dialogTitleList,dialogHandler );

        btntool.setOnClickListener ( new View.OnClickListener () {
            @Override
            public void onClick ( View v ) {
                if ( moreDialog != null ) {
                    if ( !moreDialog.isShowing () ) {
                        moreDialog.show ();
                    }
                }
            }
        } );

        initDate ();

    }

    Handler dialogHandler = new Handler (  ) {

        @Override
        public void handleMessage ( Message msg ) {
            super.handleMessage ( msg );
            switch ( msg.what ) {
                case 0:
                    //退出
                    AlertDialog dialog;
                    AlertDialog.Builder builder = new AlertDialog.Builder ( MainFrm.this );
                    builder.setTitle ( "是否要退出程序？" );
                    builder.setNegativeButton ( "取消", new OnClickListener () {
                        @Override
                        public void onClick ( DialogInterface dialog, int which ) {
                            if ( dialog != null ) {
                                dialog.dismiss ();
                            }
                        }
                    } );
                    builder.setPositiveButton ( "确定", new OnClickListener () {
                        @Override
                        public void onClick ( DialogInterface dialog, int which ) {

                            Intent intent1 = new Intent ( MainFrm.this , WatchdogService.class );
                            stopService ( intent1 );
                            Intent intent2 = new Intent ( MainFrm.this , LocationService.class );
                            stopService ( intent2 );

                            finish ();

                        }
                    } );
                    dialog = builder.create ();

                    if ( dialog != null ) {
                        if ( ! dialog.isShowing () ) {
                            dialog.show ();
                        }
                    }
                    break;
            }
        }
    };




    public void openGps () {
        AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder ( this );
        builder.setTitle ( "是否允许应用程序打开打开gps?" );
        builder.setNegativeButton ( "取消", new OnClickListener () {
            @Override
            public void onClick ( DialogInterface dialog, int which ) {
                if ( dialog != null ) {
                    dialog.dismiss ();
                }
            }
        } );
        builder.setPositiveButton ( "确定", new OnClickListener () {
            @Override
            public void onClick ( DialogInterface dialog, int which ) {
                if ( dialog != null ) {
                    dialog.dismiss ();
                }
                Intent intent = new Intent ();
                intent.setAction ( Settings.ACTION_LOCATION_SOURCE_SETTINGS );
                intent.setFlags ( Intent.FLAG_ACTIVITY_NEW_TASK );
                try {
                    startActivity ( intent );


                } catch ( ActivityNotFoundException ex ) {

                    // The Android SDK doc says that the location settings activity
                    // may not be found. In that case show the general settings.

                    // General settings activity
                    intent.setAction ( Settings.ACTION_SETTINGS );
                    try {
                        startActivity ( intent );
                    } catch ( Exception e ) {
                    }
                }
            }
        } );
        dialog = builder.create ();

        if ( dialog != null ) {
            if ( ! dialog.isShowing () ) {
                dialog.show ();
            }
        }

    }

    @Override
    protected void onResume () {
        super.onResume ();
        LocationService.addEvent ( this );
    }

    Handler singleHandler = new Handler () {

        @Override
        public void handleMessage ( Message msg ) {
            super.handleMessage ( msg );
            switch ( msg.what ) {
                case VolleyBase.SUCCESS:
                    Log.i ( "updateGPS", "上传成功!" );
                    break;
                case VolleyBase.FAILD:
                    if ( msg.obj instanceof VolleyError ) {
                        VolleyError error = ( VolleyError ) msg.obj;
                        if ( error.networkResponse != null ) {
                            if ( error.networkResponse.data != null ) {
                                String faileValue = new String ( error.networkResponse.data );
                                Gson gson = new Gson ();
                                MessageModel message = gson.fromJson ( faileValue, MessageModel.class );
                                Log.i ( "FailedValue", "value:" + message.getMsg () );
                                Toast.makeText ( MainFrm.this, message.getMsg (), Toast.LENGTH_SHORT ).show ();
                            }
                        }
                    }
                    break;
            }
        }
    };

    @Override
    protected void onDestroy () {
        super.onDestroy ();
        LocationService.removeEvent ( this );
        if ( moreDialog != null ) {
            if ( moreDialog.isShowing () ) {
                moreDialog.dismiss ();
            }
        }
    }

    //点击实现listview数据刷新，
    class listViewOnclickListener implements OnItemClickListener {

        @Override
        public void onItemClick ( AdapterView< ? > listt, View view, int position, long id ) {
            Bundle bundle = new Bundle ();
            //bundle.putString("username",username);
            //bundle.putString("id",(String) list.get(position).get("id"));
            if ( list.get ( position ).get ( "v5" ).toString ().contains ( "检修" ) || list.get ( position ).get ( "v5" ).toString ().contains ( "检修" ) ) {
                if ( list.get ( position ).get ( "v4" ).toString ().contains ( "未确认" ) ) {
                    intent.putExtras ( bundle );
                    intent.setClass ( getApplicationContext (), XJ_Task.class );
                    startActivity ( intent );
                } else if ( list.get ( position ).get ( "v4" ).toString ().contains ( "已确认" ) ) {
                    intent.putExtras ( bundle );
                    intent.setClass ( getApplicationContext (), XJ_Feedback.class );
                    startActivity ( intent );
                }
            } else {
                if ( list.get ( position ).get ( "v4" ).toString ().contains ( "未确认" ) ) {
                    intent.putExtras ( bundle );
                    intent.setClass ( getApplicationContext (), PD_Task.class );
                    startActivity ( intent );
                } else if ( list.get ( position ).get ( "v4" ).toString ().contains ( "已确认" ) ) {
                    intent.putExtras ( bundle );
                    intent.setClass ( getApplicationContext (), PD_Feedback.class );
                    startActivity ( intent );
                }
            }


        }
    }


    /**
     * 设置一个适配器(把该适配器绑定到ListView上面)
     */
    class ListAdapter extends BaseAdapter {
        private LayoutInflater inflater;

        public final class Holder {
            TextView lst1;
            TextView lst2;
            TextView lst3;
            TextView lst4;
        }

        public ListAdapter ( Context context ) {
            this.inflater = LayoutInflater.from ( context );
        }

        @Override
        public int getCount () {
            return list.size ();
        }

        @Override
        public Object getItem ( int position ) {
            return list.get ( position );
        }

        @Override
        public long getItemId ( int position ) {
            return position;
        }

        @Override
        public View getView ( final int position, View convertView, ViewGroup parent ) {
            Holder holder;
            if ( convertView == null ) {
                holder = new Holder ();
                convertView = inflater.inflate ( R.layout.listview_item, null );
                holder.lst1 = ( TextView ) convertView.findViewById ( R.id.title );
                holder.lst2 = ( TextView ) convertView.findViewById ( R.id.date );
                holder.lst3 = ( TextView ) convertView.findViewById ( R.id.content );
                holder.lst4 = ( TextView ) convertView.findViewById ( R.id.state );
                convertView.setTag ( holder );
            } else {
                holder = ( Holder ) convertView.getTag ();
            }

            holder.lst1.setText ( ( String ) list.get ( position ).get ( "v1" ) );
            holder.lst2.setText ( ( String ) list.get ( position ).get ( "v2" ) );
            holder.lst3.setText ( ( String ) list.get ( position ).get ( "v3" ) );
            holder.lst4.setText ( ( String ) list.get ( position ).get ( "v4" ) );

            if ( list.get ( position ).get ( "v4" ).toString ().contains ( "未确认" ) ) {
                holder.lst4.setTextColor ( getResources ().getColor ( R.color.state1 ) );
            } else if ( list.get ( position ).get ( "v4" ).toString ().contains ( "已确认" ) ) {
                holder.lst4.setTextColor ( getResources ().getColor ( R.color.state2 ) );
            } else if ( list.get ( position ).get ( "v4" ).toString ().contains ( "已反馈" ) ) {
                holder.lst4.setTextColor ( getResources ().getColor ( R.color.state3 ) );
            }
            return convertView;
        }
    }


    /**
     * 获取ListView的数据
     *
     * @return
     */
    private List< Map< String, Object > > getDate () {
        List< Map< String, Object > > listLitong = new ArrayList< Map< String, Object > > ();

        Map< String, Object > maplitong = new HashMap< String, Object > ();
        maplitong.put ( "v1", "检修1#道岔、2#道岔、3#道岔、7#道岔" );
        maplitong.put ( "v2", "2014年8月20日" );
        maplitong.put ( "v3", "第一小组  张三、李四、王五" );
        maplitong.put ( "v4", "未确认" );
        maplitong.put ( "v5", "检修" );
        listLitong.add ( maplitong );

        maplitong = new HashMap< String, Object > ();
        maplitong.put ( "v1", "检修4#道岔、5#道岔" );
        maplitong.put ( "v2", "2014年8月20日" );
        maplitong.put ( "v3", "第一小组  张三、李四、王五" );
        maplitong.put ( "v4", "已确认" );
        maplitong.put ( "v5", "检修" );
        listLitong.add ( maplitong );

        maplitong = new HashMap< String, Object > ();
        maplitong.put ( "v1", "巡视 RBC、联锁" );
        maplitong.put ( "v2", "2014年8月20日" );
        maplitong.put ( "v3", "第二小组  万名、李想、张静" );
        maplitong.put ( "v4", "未确认" );
        maplitong.put ( "v5", "巡视" );
        listLitong.add ( maplitong );

        maplitong = new HashMap< String, Object > ();
        maplitong.put ( "v1", "巡视 RBC、联锁" );
        maplitong.put ( "v2", "2014年8月20日" );
        maplitong.put ( "v3", "第二小组  万名、李想、张静" );
        maplitong.put ( "v4", "已确认" );
        maplitong.put ( "v5", "巡视" );
        listLitong.add ( maplitong );

        maplitong = new HashMap< String, Object > ();
        maplitong.put ( "v1", "盘点 长沙南站 备品备件" );
        maplitong.put ( "v2", "2014年8月20日" );
        maplitong.put ( "v3", "第一小组  张三、李四、王五" );
        maplitong.put ( "v4", "未确认" );
        maplitong.put ( "v5", "盘点" );
        listLitong.add ( maplitong );

        maplitong = new HashMap< String, Object > ();
        maplitong.put ( "v1", "盘点 长沙南站 备品备件" );
        maplitong.put ( "v2", "2014年8月20日" );
        maplitong.put ( "v3", "第一小组  张三、李四、王五" );
        maplitong.put ( "v4", "已确认" );
        maplitong.put ( "v5", "盘点" );
        listLitong.add ( maplitong );


        maplitong = new HashMap< String, Object > ();
        maplitong.put ( "v1", "检修4#道岔、5#道岔" );
        maplitong.put ( "v2", "2014年8月20日" );
        maplitong.put ( "v3", "第一小组  张三、李四、王五" );
        maplitong.put ( "v4", "已反馈" );
        maplitong.put ( "v5", "检修" );
        listLitong.add ( maplitong );

        maplitong = new HashMap< String, Object > ();
        maplitong.put ( "v1", "巡视 RBC、联锁" );
        maplitong.put ( "v2", "2014年8月20日" );
        maplitong.put ( "v3", "第二小组  万名、李想、张静" );
        maplitong.put ( "v4", "已反馈" );
        maplitong.put ( "v5", "巡视" );
        listLitong.add ( maplitong );

        maplitong = new HashMap< String, Object > ();
        maplitong.put ( "v1", "巡视 RBC、联锁" );
        maplitong.put ( "v2", "2014年8月20日" );
        maplitong.put ( "v3", "第二小组  万名、李想、张静" );
        maplitong.put ( "v4", "已反馈" );
        maplitong.put ( "v5", "巡视" );
        listLitong.add ( maplitong );

        maplitong = new HashMap< String, Object > ();
        maplitong.put ( "v1", "盘点 长沙南站 备品备件" );
        maplitong.put ( "v2", "2014年8月20日" );
        maplitong.put ( "v3", "第一小组  张三、李四、王五" );
        maplitong.put ( "v4", "已反馈" );
        maplitong.put ( "v5", "盘点" );
        listLitong.add ( maplitong );

        maplitong = new HashMap< String, Object > ();
        maplitong.put ( "v1", "盘点 长沙南站 备品备件" );
        maplitong.put ( "v2", "2014年8月20日" );
        maplitong.put ( "v3", "第一小组  张三、李四、王五" );
        maplitong.put ( "v4", "已反馈" );
        maplitong.put ( "v5", "盘点" );
        listLitong.add ( maplitong );

        return listLitong;
    }

    /**
     * 拿到整个数据List的大小</br> 如果List不等于空 或者 List的大小大于零</br>
     * 实例化一个自定义的适配器,把自定义的适配器绑定到控件ListView上面</br>
     * 实例化一个自定义的监听器,把自定义的监听器绑定到控件ListView上面</br> 判断屏幕的密度 </br> 如果密度小于1.0
     * 密度等于1.5</br> 就给ListView手动设置宽度和高度</br>
     */
    private void initDate () {
        list = getDate ();
        if ( list != null && list.size () > 0 ) {
            adapter = new ListAdapter ( this );
            super.listViewAll.setAdapter ( adapter );
            super.listViewAll.setOnItemClickListener ( new listViewOnclickListener () );
        }
    }

    public void btn_shebei_click ( View v ) {

        Intent intent = new Intent ();
        intent.setClass ( getApplicationContext (), PD_Shaomiao.class );
        startActivity ( intent );
    }

}
    
    

